﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;



namespace frag_boardgame
{

    [DataContract]
    public class Configuraion
    {

        GameConfiguration gameConf = new GameConfiguration();
        List<PlayerConfiguration> playerConfs = new List<PlayerConfiguration>();
        Board board = null;

        [DataMember]
        public GameConfiguration GameConfiguration
        {
            get { return gameConf; }
            set { gameConf = value; }
        }

        [DataMember]
        public List<PlayerConfiguration> PlayerConfigurations
        {
            get { return playerConfs; }
            set { playerConfs = value; }
        }

        public int NumberOfPlayers
        { 
            get { return gameConf.NumberOfPlayers; } 
        }

        Random random = new Random();

        public Configuraion()
        {
            gameConf = new GameConfiguration();
            gameConf.Cols = 25;
            gameConf.Rows = 16;
            gameConf.NumberOfPlayers = 2;
        }

        public Configuraion(GameConfiguration gameConf)
        {
            this.gameConf = gameConf;
        }

  
        /// <summary>
        /// Generate a randomized pos
        /// </summary>
        /// <returns></returns>
        private Pos GenerateRandomPos()
        {
            bool stopLooking = false;

            Pos generatedPos = new Pos(-1, -1);
            
            while (!stopLooking)
            {
                generatedPos.row = random.Next(0, Rows);
                generatedPos.col = random.Next(0, Cols);

                stopLooking = true;

                foreach (PlayerConfiguration player in PlayerConfigurations)
                {
                    if ((generatedPos.col == player.StartingPosition.col) && (generatedPos.row == player.StartingPosition.row))
                        stopLooking = false;
                }


            }
            return generatedPos;
            
        }
        
        public int Rows
        {
            get
            {
                return gameConf.Rows;
            }
            set
            {
                if (value > 0)
                    gameConf.Rows = value;
                
            }
        }

        public int Cols
        {
            get
            {
                return gameConf.Cols;
            }
            set
            {
                if (value > 0)
                    gameConf.Cols = value;
            }
        }

        public void addPlayer(PlayerConfiguration pl)
        {
            if (pl.StartingPosition == null || pl.StartingPosition.row == -1 || pl.StartingPosition.col == -1)
            {
                pl.StartingPosition = GenerateRandomPos();
            }
            
            playerConfs.Add(pl);
        }

        public Board CreateBoard()
        {
            if (this.board == null)
                return new Board(this.Rows, this.Cols);
            return board;
        }

        public void SetBoard(Board board)
        {
            this.board = board;
            this.Cols = board.Width;
            this.Rows = board.Height;
        }
    }
}
